<html>
<head>
    <script type="text/javascript">var oldAlert = window.alert, gMess; window.alert = function(message) {gMess = message; return true;};</script>
    <script type="text/javascript">window.alert = oldAlert;</script>
    

<script src="../OLLoader.js"></script>
  <script type="text/javascript">
    // turn off animation frame handling, so we can check img urls in tests
    delete OpenLayers.Layer.Grid.prototype.queueTileDraw;
    
    var isMozilla = (navigator.userAgent.indexOf("compatible") == -1);
    var layer; 

    var name = 'Test Layer';
    var url = "http://labs.metacarta.com/cgi-bin/mapserv";
    var params = { map: '/mapdata/vmap_wms.map', 
                   layers: 'basic'};

    function test_Layer_MapServer_constructor (t) {
        t.plan( 4 );

        var url = "http://labs.metacarta.com/cgi-bin/mapserv";
        layer = new OpenLayers.Layer.MapServer(name, url, params);
        t.ok( layer instanceof OpenLayers.Layer.MapServer, "new OpenLayers.Layer.MapServer returns object" );
        t.eq( layer.url, "http://labs.metacarta.com/cgi-bin/mapserv", "layer.url is correct (HTTPRequest inited)" );

        t.eq( layer.params.mode, "map", "default mode param correctly copied");
        t.eq( layer.params.map_imagetype, "png", "default imagetype correctly copied");


    }
    
    function test_Layer_MapServer_addtile (t) {
        t.plan( 6 );
    
        var url = "http://labs.metacarta.com/cgi-bin/mapserv";
        layer = new OpenLayers.Layer.MapServer(name, url, params);
        var map = new OpenLayers.Map('map', {tileManager: null});
        map.addLayer(layer);
        var pixel = new OpenLayers.Pixel(5,6);
        var tile = layer.addTile(new OpenLayers.Bounds(1,2,3,4), pixel);
        tile.draw();

        var img = tile.imgDiv;
        var tParams = OpenLayers.Util.extend({},params);
        tParams = OpenLayers.Util.extend(tParams, {
                 layers: 'basic', 
                 mode: 'map',
                 map_imagetype: 'png',
                 mapext:[1,2,3,4],
                 imgext:[1,2,3,4],
                 map_size:[256, 256],
                 imgx:128,
                 imgy:128,
                 imgxy:[256,256]
        });
        t.eq( tile.url,
             url + "?" + OpenLayers.Util.getParameterString(tParams).replace(/,/g, "+"),
             "image src is created correctly via addtile" );
        t.eq( tile.getTile().style.top, "6px", "image top is set correctly via addtile" );
        t.eq( tile.getTile().style.left, "5px", "image top is set correctly via addtile" );

        var firstChild = layer.div.firstChild;
        t.eq( firstChild.nodeName.toLowerCase(), "img", "div first child is an image object" );
        t.ok( firstChild == img, "div first child is correct image object" );
        t.eq( tile.position.toString(), "x=5,y=6", "Position of tile is set correctly." );
        map.destroy();
    }
    
    function test_Layer_MapServer_inittiles (t) {
        t.plan( 2 );
        var map = new OpenLayers.Map('map');
        layer = new OpenLayers.Layer.MapServer(name, url, params, {buffer: 0});
        map.addLayer(layer);
        map.setCenter(new OpenLayers.LonLat(0,0),5);
        t.eq( layer.grid.length, 4, "Grid rows is correct." );
        t.eq( layer.grid[0].length, 3, "Grid cols is correct." );
        map.destroy();
        
    }


    function test_Layer_MapServer_clone (t) {
        t.plan(4);
        
        var url = "http://labs.metacarta.com/cgi-bin/mapserv";
        var options = {tileSize: new OpenLayers.Size(500,50)};
        var map = new OpenLayers.Map('map', options);
        layer = new OpenLayers.Layer.MapServer(name, url, params);
        map.addLayer(layer);

        layer.grid = [ [6, 7], 
                       [8, 9]];

        var clone = layer.clone();

        t.ok( clone.grid != layer.grid, "clone does not copy grid");

        t.ok( clone.tileSize.equals(layer.tileSize), "tileSize correctly cloned");

        layer.tileSize.w += 40;

        t.eq( clone.tileSize.w, 500, "changing layer.tileSize does not change clone.tileSize -- a fresh copy was made, not just copied reference");

        t.eq( clone.alpha, layer.alpha, "alpha copied correctly");

        layer.grid = null;
        map.destroy();
    }

    function test_Layer_MapServer_isBaseLayer(t) {
        t.plan(3);
        
        var url = "http://labs.metacarta.com/cgi-bin/mapserv";
        layer = new OpenLayers.Layer.MapServer(name, url, params);
        t.ok( layer.isBaseLayer, "baselayer is true by default");

        var newParams = OpenLayers.Util.extend({}, params);
        newParams.transparent = "true";
        layer = new OpenLayers.Layer.MapServer(name, url, newParams);
        t.ok( !layer.isBaseLayer, "baselayer is false when transparent is set to true");

        layer = new OpenLayers.Layer.MapServer(name, url, params, {isBaseLayer: false});
        t.ok( !layer.isBaseLayer, "baselayer is false when option is set to false" );
    }

    function test_Layer_MapServer_mergeNewParams (t) {
        t.plan( 4 );

        var map = new OpenLayers.Map("map");
        var url = "http://labs.metacarta.com/cgi-bin/mapserv";
        layer = new OpenLayers.Layer.MapServer(name, url, params);
        
        var newParams = { layers: 'sooper', 
                          chickpeas: 'image/png'};

        map.addLayer(layer);
        map.zoomToMaxExtent();
        
        layer.redraw = function() {
            t.ok(true, "layer is redrawn after new params merged");
        }
        layer.mergeNewParams(newParams);
        
        t.eq( layer.params.layers, "sooper", "mergeNewParams() overwrites well");
        t.eq( layer.params.chickpeas, "image/png", "mergeNewParams() adds well");
        
        newParams.chickpeas = 151;

        t.eq( layer.params.chickpeas, "image/png", "mergeNewParams() makes clean copy of hashtable");
        map.destroy();
    }

    function test_Layer_MapServer_getFullRequestString (t) {
        t.plan( 3 );
        var map = new OpenLayers.Map('map');
        tUrl = "http://labs.metacarta.com/cgi-bin/mapserv";
        tParams = { layers: 'basic', 
                    format: 'png'};
        var tLayer = new OpenLayers.Layer.MapServer(name, tUrl, tParams);
        map.addLayer(tLayer);
        str = tLayer.getFullRequestString();
        var tParams = {
                 layers: 'basic', 
                 format: 'png',
                 mode: 'map',
                 map_imagetype: 'png'
        };

            var sStr = tUrl + "?" + OpenLayers.Util.getParameterString(tParams);
            sStr = sStr.replace(/,/g, "+");
        
        t.eq(str, sStr , "getFullRequestString() works");
        map.destroy();
        
        tUrl = ["http://octo.metacarta.com/cgi-bin/mapserv","http://labs.metacarta.com/cgi-bin/mapserv"];
        layer = new OpenLayers.Layer.MapServer(name, tUrl, tParams, null);
        str = layer.getFullRequestString({'c':'d'});
        t.eq(str, tUrl[1] + '?' + OpenLayers.Util.getParameterString(OpenLayers.Util.extend(tParams,{'c':'d'})), "getFullRequestString() works for list of two urls and is deterministic");
        layer.destroy();
        var tParams = {
                 layers: 'basic', 
                 format: 'png',
                 mode: 'map',
                 map_imagetype: 'png'
        };
        tUrl = ["http://octo.metacarta.com/cgi-bin/mapserv","http://labs.metacarta.com/cgi-bin/mapserv"];
        layer = new OpenLayers.Layer.MapServer(name, tUrl, tParams, null);
        str = layer.getFullRequestString({'a':'b'});
        t.eq(str, tUrl[0] + '?' + OpenLayers.Util.getParameterString(OpenLayers.Util.extend(tParams,{'a':'b'})), "getFullRequestString() works for list of two urls and is deterministic");
        layer.destroy();

    }

    function test_Layer_MapServer_singleTile (t) {
        t.plan( 5 );
        var map = new OpenLayers.Map('map');
        layer = new OpenLayers.Layer.MapServer(name, url, params, {singleTile: true});
        map.addLayer(layer);
        map.setCenter(new OpenLayers.LonLat(0,0),5);
        t.eq( layer.singleTile, true, "layer has singleTile property, great!" );
        t.eq( layer.grid.length, 1, "Grid has only a single row, good enough!" );
        t.eq( layer.grid[0].length, 1, "Grid has only a single column, good enough!" );
        t.eq( layer.tileSize.w, 750, "Image width is correct" );
        t.eq( layer.tileSize.h, 825, "Image height is correct" );
        map.destroy();
    }

    
    
    function test_Layer_MapServer_destroy (t) {

        t.plan( 1 );

        var map = new OpenLayers.Map('map');
        layer = new OpenLayers.Layer.MapServer(name, url, params);
        map.addLayer(layer);

        map.setCenter(new OpenLayers.LonLat(0,0), 5);

        //grab a reference to one of the tiles
        var tile = layer.grid[0][0];        

        layer.destroy();
        
    // checks to make sure superclass (grid) destroy() was called    
        
        t.ok( layer.grid == null, "grid set to null");
        map.destroy();
    }
    
  </script>
</head>
<body>
<div id="map" style="width:500px;height:550px"></div>
</body>
</html>
